package info.vanbeem.WikiMindDesktop;

import java.io.File;
import java.io.IOException;
import java.util.prefs.Preferences;

import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Shell;

class Settings {
	static final String ROOT_PATH = "rootPath";
	static final String TEMPLATE_FILE = "templateFile";
	static final String POSITION_X = "positionX";
	static final String POSITION_Y = "positionY";
	static final String POSITION_WIDTH = "positionWidth";
	static final String POSITION_HEIGHT = "positionHeight";
	static final String SELECTED_ITEM = "selectedItem";
	
	Preferences node = Preferences.userNodeForPackage(this.getClass());
	
	public Settings() {
		loadTemplate(getTemplateFile());
	}
	
	public void getDimensions(Shell window) {
		window.setBounds(node.getInt(POSITION_X, 0), node.getInt(POSITION_Y, 0),
				node.getInt(POSITION_WIDTH, 580), node.getInt(POSITION_HEIGHT, 740));
	}
	
	public void setDimensions(Shell window) {
		Rectangle bounds = window.getBounds();
		node.putInt(POSITION_X, bounds.x);
		node.putInt(POSITION_Y, bounds.y);
		node.putInt(POSITION_WIDTH, bounds.width);
		node.putInt(POSITION_HEIGHT, bounds.height);
	}
	
	public String getSelectedItem() {
		return node.get(SELECTED_ITEM, "");
	}

	public void setSelectedItem(String item) {
		node.put(SELECTED_ITEM, item);
	}
	
	public String getRootPath() {
		return node.get(ROOT_PATH, "C:/Users/patrickvb/Documents/My Dropbox/WikiMind/notes");
	}

	public void setRootPath(String rootPath) {
		node.put(ROOT_PATH, rootPath);
	}
	
	String getTemplateFile() {
		return node.get(TEMPLATE_FILE, "C:/Users/patrickvb/workspace/WikiMindDesktop/template.html");
	}

	void setTemplateFile(String templateFile) {
		node.put(TEMPLATE_FILE, templateFile);
		loadTemplate(templateFile);
	}
	
	private void loadTemplate(final String templateFile) {
		template = null;
		if (templateFile != null) {
			File file = new File(templateFile);
			try {
				template = Utils.readFile(file);
			} catch (IOException e) {
			}
		}
	}
	private String template;

	String getTemplate() {
		return template;
	}

}
